<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Categories.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Model_DbTable_Categories extends Engine_Db_Table {
	
	protected $_rowClass = 'Sitepage_Model_Category';
	
	/**
	 * Return subcaregories
	 *
	 * @param int category_id
	 * @return all sub categories
	 */
	public function getSubCategories($category_id) {
		if (empty ( $category_id )) {
			return;
		}
		$select = $this->select ()->from ( $this->info ( 'name' ), array ('category_name', 'category_id', 'cat_order', 'cat_dependency' ) )->where ( 'cat_dependency = ?', $category_id )->order ( 'cat_order' );
		return $this->fetchAll ( $select );
	}
	
	/**
	 * Return category name for the page
	 *
	 * @param int category_id
	 * @return Zend_Db_Table_Select
	 */
	public function getCategory($category_id) {
		if (empty ( $category_id )) {
			return;
		}
		$select = $this->select ()->from ( $this->info ( 'name' ), array ('category_name', 'category_id' ) )->where ( 'category_id =?', $category_id )->order ( 'cat_order' );
		return $this->fetchRow ( $select );
	}
	
	/**
	 * Return categories
	 *
	 * @param int $home_page_display
	 * @return categories
	 */
	public function getCategories($home_page_display = 0) {
		$cateName = $this->info ( 'name' );
		$select = $this->select ()->where ( 'cat_dependency =?', 0 )->order ( 'cat_order' );
		if (! empty ( $home_page_display )) {
			$table = Engine_Api::_ ()->getDbtable ( 'pages', 'sitepage' );
			$rName = $table->info ( 'name' );
			$select->setIntegrityCheck ( false )->from ( $cateName )->joinLeft ( $rName, $rName . '.category_id = ' . $cateName . '.category_id', array ('count(' . $rName . '.category_id ) as count' ) )->group ( $cateName . '.category_id' )->order ( 'cat_order ASC' );
		}
		return $this->fetchAll ( $select );
	}
	
	/**
	 * Return slug
	 *
	 * @param int $categoryname
	 * @return categoryname
	 */
	public function getCategorySlug($categoryname) {
		$showslug = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.categorywithslug', 1 );
		if (! empty ( $showslug )) {
			return trim ( preg_replace ( '/-+/', '-', preg_replace ( '/[^a-z0-9-]+/i', '-', strtolower ( $categoryname ) ) ), '-' );
		} else {
			return $categoryname;
		}
	}
	
	/**
	 * Gets all categories and subcategories
	 *
	 * @param string $category_id
	 * @param string $fieldname
	 * @param int $pageCondition
	 * @param string $page
	 * @param  all categories and subcategories
	 */
	public function getAllCategories($category_id, $fieldname, $pageCondition, $page, $subcat = null, $limit = 0) {
		$tableCategoriesName = $this->info ( 'name' );
		$tablePageName = Engine_Api::_ ()->getDbtable ( 'pages', 'sitepage' )->info ( 'name' );
		$select = $this->select ()->setIntegrityCheck ( false )->from ( $tableCategoriesName );
		if ($subcat == 1) {
			$select = $select->joinLeft ( $tablePageName, $tablePageName . '.' . $fieldname . '=' . $tableCategoriesName . '.category_id', array ('count(DISTINCT ' . $tablePageName . '.' . $page . ' ) as count' ) );
		} else {
			$select = $select->joinLeft ( $tablePageName, $tablePageName . '.' . $fieldname . '=' . $tableCategoriesName . '.category_id', array ('count(' . $tablePageName . '.' . $fieldname . ' ) as count' ) );
		}
		
		$select = $select->where ( $tableCategoriesName . '.cat_dependency = ' . $category_id )->group ( $tableCategoriesName . '.category_id' )->order ( 'cat_order' );
		
		if (! empty ( $limit )) {
			$select = $select->limit ( $limit );
		}
		
		if ($pageCondition == 1) {
			$select->where ( $tablePageName . '.closed = ?', '0' )->where ( $tablePageName . '.approved = ?', '1' )->where ( $tablePageName . '.draft = ?', '1' );
			
			$select->where ( $tablePageName . ".search = ?", 1 );
			
			//START NETWORK WORK
			if ($subcat == 1) {
				$enableNetwork = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.network', 0 );
				if ($enableNetwork) {
					$viewer = Engine_Api::_ ()->user ()->getViewer ();
					$networkMembershipTable = Engine_Api::_ ()->getDbtable ( 'membership', 'network' );
					if (! Engine_Api::_ ()->getApi ( 'subCore', 'sitepage' )->pageBaseNetworkEnable ()) {
						$viewerNetwork = $networkMembershipTable->fetchRow ( array ('user_id = ?' => $viewer->getIdentity () ) );
						if (! empty ( $viewerNetwork )) {
							$networkMembershipName = $networkMembershipTable->info ( 'name' );
							$select->join ( $networkMembershipName, "`{$tablePageName}`.owner_id = `{$networkMembershipName}`.user_id  ", null )->join ( $networkMembershipName, "`{$networkMembershipName}`.`resource_id`=`{$networkMembershipName}_2`.resource_id", null )->where ( "`{$networkMembershipName}_2`.user_id = ? ", $viewer->getIdentity () );
						}
					} else {
						$viewerNetwork = $networkMembershipTable->getMembershipsOfInfo ( $viewer );
						$str = array ();
						$columnName = "`{$tablePageName}`.networks_privacy";
						foreach ( $viewerNetwork as $networkvalue ) {
							$network_id = $networkvalue->resource_id;
							$str [] = "'" . $network_id . "'";
							$str [] = "'" . $network_id . ",%'";
							$str [] = "'%," . $network_id . ",%'";
							$str [] = "'%," . $network_id . "'";
						}
						if (! empty ( $str )) {
							$likeNetworkVale = ( string ) (join ( " or $columnName  LIKE ", $str ));
							$select->where ( $columnName . ' LIKE ' . $likeNetworkVale . ' or ' . $columnName . " IS NULL" );
						} else {
							$select->where ( $columnName . " IS NULL" );
						}
					}
				}
			}
			//END NETWORK WORK
			

			return $this->fetchAll ( $select );
		} else {
			return $this->fetchAll ( $select );
		}
	}

}
?>